/*
 * CRtpEsMediaCaster.h
 *
 *  Created on: 2018年5月12日
 *      Author: zhengboyuan
 */

#ifndef CRTPESMEDIACASTER_H_
#define CRTPESMEDIACASTER_H_

#include "PsMediaCaster.h"
#include "RtpMediaCaster.h"
#include "H264PropDetector.h"
#include "NaluJoiner.h"
#include "TCriticalSection.h"
#include "TcpClient.h"

namespace av
{

typedef std::shared_ptr< RtpMediaCaster >	RtpMediaCasterPtr;

class CRtpEsMediaCaster : public RtpPsMediaCaster, public TcpClient::Sink
{
public:
	explicit CRtpEsMediaCaster(int protocol);
	~CRtpEsMediaCaster();

	virtual int open(const std::string& ip, uint16_t port, const MediaFormat& fmt);

	virtual void close();

	virtual bool isOpen();

	virtual void setID(int id);

	virtual int getID();

	virtual bool addTarget(const std::string& ip, uint16_t port);

	virtual void removeTarget(const std::string& ip, uint16_t port);

	virtual size_t getTargetCount();

	virtual void removeAllTarget();

	virtual bool write(av::MediaPacketPtr& pkt);


	virtual void setPayload(uint8_t payload);

	virtual void setSsrc(uint32_t ssrc);


protected:
    virtual void onTcpEvent(void* obj, int event);

protected:
	bool write(int type, uint8_t* data, int size, int64_t pts, int duration, int flags);
	void writePacket(int type, uint8_t* data, int size, int64_t pts, int flags);

protected:

	int	m_protocol;
	RtpMediaCasterPtr	m_caster;
	uint8_t		m_payload;
	uint32_t	m_ssrc;

	H264PropDetector	m_propDetector;
	std::shared_ptr< NaluJoiner >	m_naluJoiner;

	MediaFormat	m_format;
	comn::CriticalSection	m_cs;

	int64_t	m_videoLastPts;
	int64_t m_audioLastPts;

	int		m_id;
};


} /* namespace av */

#endif /* CRTPESMEDIACASTER_H_ */
